package com.ctshk.sapp.order.custom.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.TokenUser;
import com.ctshk.rpc.order.custom.dto.order.CustomOrderDetailDTO;
import com.ctshk.rpc.order.custom.dto.order.CustomOrderListDTO;
import com.ctshk.rpc.order.custom.dto.order.CustomOrderUpdateContactDTO;
import com.ctshk.rpc.order.custom.req.order.CustomOrderIdReq;
import com.ctshk.rpc.order.custom.req.order.CustomOrderListReq;
import com.ctshk.rpc.order.custom.req.order.CustomOrderUpdateContactReq;
import com.ctshk.rpc.order.custom.req.schedule.TripScheduleCreateOrderReq;
import com.ctshk.rpc.order.custom.service.ICustomizedOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.ctshk.common.web.support.BaseController;

import javax.validation.Valid;

/**
 * <p>
 * 定制包团订单 前端控制器
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-22
 */
@Api(value="订单", tags="订单")
@RestController
@Slf4j
@RequestMapping("/customized-order")
public class CustomizedOrderController extends BaseController {

    @DubboReference
    private ICustomizedOrderService orderService;


    @ApiOperation(value="定制包团下单", notes="定制包团下单(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "定制包团下单", operDesc = "定制包团下单")
    @PostMapping("/add")
    public Result createOrder(@RequestBody @Valid TripScheduleCreateOrderReq tripScheduleCreateOrderReq) {
        try {
            TokenUser user = getCurrentUser(getRequest());
            return orderService.createOrder(tripScheduleCreateOrderReq,user.getId());
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "订单列表", notes = "订单列表（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单列表", operDesc = "订单列表")
    @PostMapping("/list")
    public Result<CustomOrderListDTO> list(@RequestBody CustomOrderListReq req) {
        try {
            TokenUser currentUser = getCurrentUser(getRequest());
            req.setUserId(currentUser.getId());
            return orderService.list(req,currentUser);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "取消订单", notes = "取消订单（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "取消订单", operDesc = "取消订单")
    @PostMapping("/cancel")
    public Result cancel(@RequestBody @Valid CustomOrderIdReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderService.cancel(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "订单详情", notes = "订单详情（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "订单详情", operDesc = "订单详情")
    @PostMapping("/detail")
    public Result<CustomOrderDetailDTO> detail(@RequestBody @Valid CustomOrderIdReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderService.detail(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "修改联络人信息", notes = "操作订单-修改联络人信息（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "操作订单-修改联络人信息", operDesc = "操作订单-修改联络人信息")
    @PostMapping("/update-contact")
    public Result<CustomOrderUpdateContactDTO> updateContact(@RequestBody @Valid CustomOrderUpdateContactReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderService.updateContact(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }


}
